<!--
 * @Author: xuman
 * @Date: 2020-05-25 14:57:43
 * @LastEditors: xuman
 * @LastEditTime: 2020-06-02 10:22:36
-->
<template>
  <el-dialog
    title="参数详情"
    :close-on-click-modal="false"
    :modal-append-to-body="false"
    :visible="true"
    width="60%"
    :before-close="handleClose"
    class="detail"
  >
    <div style="display: flex;width: 100%">
      <div style="width: 60%">
        <el-tabs v-model="activeName">
          <el-tab-pane label="输入参数" name="input">
            <el-table
              ref="multipleTable"
              :data="inData"
              fit
              highlight-current-row
              :max-height="500"
            >
              <el-table-column
                label="参数CODE"
                min-width="130px"
                prop="columnName"
                align="center"
              />
              <el-table-column
                label="数据类型"
                min-width="160px"
                prop="columnType"
                align="center"
              />
              <el-table-column
                fixed="right"
                label="输入值"
                align="center"
                width="180px"
              >
                <template slot-scope="scope">
                  <el-input
                    type="text"
                    size="mini"
                    v-model.trim="scope.row.value"
                    :placeholder="scope.row.required ? '必填' : ''"
                  />
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              @current-change="handleCurrentChange"
              :current-page.sync="page.page"
              :page-size="page.pageSize"
              layout="total, prev, pager, next"
              :total="page.total"
            >
            </el-pagination>
          </el-tab-pane>
          <el-tab-pane label="输出参数" name="output">
            <el-table
              ref="multipleTable"
              :data="outData"
              fit
              highlight-current-row
              :max-height="500"
            >
              <el-table-column
                label="出参CODE"
                min-width="130px"
                prop="columnName"
                align="center"
              />
              <el-table-column
                label="出参名称"
                min-width="160px"
                prop="display"
                align="center"
              />
              <el-table-column
                label="出参类型"
                min-width="160px"
                prop="columnType"
                align="center"
              />
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </div>
      <div style="width: 40%;padding-left: 12px">
        <el-form
          class="small-space"
          ref="featureForm"
          label-position="top"
          label-width="84px"
        >
          <el-form-item label="请求内容">
            <pre class="detail-pre">{{ formatInParams }}</pre>
          </el-form-item>
          <el-form-item label="返回内容">
            <pre class="detail-pre">{{ formatOutParams }}</pre>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button size="mini" @click="handleClose">取消</el-button>
      <el-button size="mini" type="primary" @click="handleDebug">
        测试
      </el-button>
    </div>
  </el-dialog>
</template>
<script lang="ts">
import { Component, Vue, Prop, Watch } from 'vue-property-decorator'
import { debounce } from 'lodash'
import * as Api from '@/api/risksystem/feature/model'

interface IPage {
  page: number
  pageSize: number
  total: number
}

interface IInData {
  id: number
  columnName: string
  columnType: string
  value?: string
}

interface IOutData {
  columnName: string
  columnType: string
  display: string
}

interface IParamObj {
  [key: string]: string | undefined
}

@Component({})
export default class ParamsDetail extends Vue {
  @Prop() private modelCode!: string

  private parameterVisible: boolean = false
  private activeName: string = 'input'
  private inData: IInData[] = []
  private totalInData: any = {}
  private outData: IOutData[] = []
  private formatOutParams: string = ''
  private page: IPage = {
    page: 0,
    pageSize: 10,
    total: 0
  }

  @Watch('inData', { deep: true })
  private handleDataChange(val: IInData[]) {
    const { page } = this.page
    this.$set(this.totalInData, page, val)
  }

  created() {
    this.getModelInParam()
    this.getModelOutParam()
  }

  get formatInParams() {
    const data = this.flatten(Object.values(this.totalInData))
    return this.getFormatJson(data)
  }
  private flatten(arr: any) {
    let arr1: any = []
    arr.forEach((val: any) => {
      if (Array.isArray(val)) {
        arr1 = arr1.concat(val)
      } else {
        arr1.push(val)
      }
    })
    return arr1
  }
  // 格式化参数
  private getFormatJson(data: IInData[]) {
    const paramObj: IParamObj = {}
    data
      .filter(item => !!item.value)
      .map(item => {
        paramObj[item.columnName] = item.value
      })
    return JSON.stringify(paramObj, null, 4)
  }

  // 获取入参
  private async getModelInParam() {
    const {
      data: { records, current: page, total }
    } = await Api.getModelInParam({
      modelCode: this.modelCode,
      ...this.page
    })
    this.inData = records
    this.page = { ...this.page, page, total }
  }

  // 获取出参
  private async getModelOutParam() {
    const { data } = await Api.getModelOutParam({ modelCode: this.modelCode })
    this.outData = data
  }

  // 调式模型
  private handleDebug() {
    debounce(async () => {
      const input = JSON.parse(this.formatInParams)
      if (Object.keys(input).length !== this.page.total) {
        return this.$message.warning('请完善必填参数！')
      }
      const { modelCode, formatInParams } = this
      const { data } = await Api.debugModel({
        modelCode,
        input
      })
      this.formatOutParams = JSON.stringify(data, null, 4)
    }, 1000)()
  }

  // 分页
  private handleCurrentChange(val: number) {
    this.page.page = val
    this.totalInData.hasOwnProperty(val)
      ? (this.inData = this.totalInData[val])
      : this.getModelInParam()
  }

  private handleClose() {
    this.$emit('handleClose')
  }
}
</script>
<style lang="scss" scoped>
.detail {
  &-pre {
    display: block;
    width: 100%;
    min-height: 135px;
    max-height: 250px;
    overflow: auto;
    border: 1px solid #dcdfe6;
    margin: 0;
    border-radius: 5px;
    padding: 0 10px;
  }
}
</style>
